-
2021-02-04 12:15:10
这不是我的全部代码,但是我发布了与我要执行的操作有关的内容:
from tkinter import *
from random import seed
from random import randint
class screens:
def __init__(self, title, size, bg):
self.title = title
self.size = size
self.bg = bg
def if_right4():
if square4["text"] == 'hi':
print('Yes')
else:
print("No")
def screen_one():
window1 = Tk()
global square1
global square2
global square3
global square4
window1.title(s1.title) # Sets window title
window1.geometry(s1.size) # Sets window size
window1.configure(bg=s1.bg) # Changes background
window1.iconbitmap(default='Images/favicon.ico') # Removes the 'feather'
window1.resizable(0,0)
# Button(object1, text="Enter", width=15, height=2, command=quit).place(x=0, y=0)
TopFrame = Frame(window1, bg = '#273c75', width = 950, height = 50).place(x=0,y=0)
TopFrame2 = Frame(window1, bg = '#192a56', width = 950, height = 100).place(x=0,y=40)
streakLabel = Label(window1, fg = 'black', bg = '#ffffff', font = 'Helvetica 20 bold', text = 'Streak:').place(x=400,y=430)
displayStreak = Label(window1, fg = 'black', bg = '#ffffff', font = 'Helvetica 22 bold', text = 0).place(x=435,y=470)
displayQuestion = Label(window1, fg = '#ffffff', bg = '#273c75', font = 'Helvetica 22 bold', text = displayedQuestion).place(x=0,y=0)
square1 = Button(window1, bg = '#fbc531', fg = 'white', width = 34, height = 17, text = displayedOption1, font = 'Helvetica 12 bold',
bd=0, highlightthickness=0 ).place(x=0,y=140)
square2 = Button(window1, bg = '#4cd137', fg = 'white', width = 34, height = 17, text = displayedOption2, font = 'Helvetica 12 bold',
bd=0, highlightthickness=0 ).place(x=0,y=520)
square3 = Button(window1, bg = '#e84118', fg = 'white', width = 34, height = 17, text = displayedOption3, font = 'Helvetica 12 bold',
bd=0, highlightthickness=0 ).place(x=560,y=520)
square4 = Button(window1, bg = '#00a8ff', fg = 'white', width = 34, height = 17, text = displayedOption4, font = 'Helvetica 12 bold',
bd=0, highlightthickness=0, command=if_right4).place(x=560,y=140)
window1.mainloop()
“ displayedOption”变量等于文本文件中的随机行。
我得到的错误是TypeError: 'NoneType' object is not subscriptable 我的目标是尝试获取按钮上的文本作为变量,以便我可以比较两个值。任何帮助,将不胜感激。
更多相关内容 -
Python3 tkinter基础 Button text,fg 按钮上显示的文字 文字的颜色
2020-12-18 10:27:34Python : 3.7.0OS : Ubuntu 18.04.1 LTSIDE : PyCharm 2018.2.4Conda : 4.5.11typesetting : Markdowncode"""@Author : 行初心@Date : 18-10-1@Blog : ...Python : 3.7.0
OS : Ubuntu 18.04.1 LTS
IDE : PyCharm 2018.2.4
Conda : 4.5.11
typesetting : Markdown
code
"""
@Author : 行初心
@Date : 18-10-1
@Blog : www.cnblogs.com/xingchuxin
@Gitee : gitee.com/zhichengjiu
"""
import tkinter as tk
# 面向对象的编程思想
class APP:
def __init__(self, master):
self.frame = tk.Frame(master)
self.frame.pack()
# 新建一个按钮 上面的显示:佛手柑 前景色(字体颜色):蓝色
self.hi_there = tk.Button(frame, text="佛手柑", fg="blue")
self.hi_there.pack()
def main():
root = tk.Tk()
app = APP(root)
root.mainloop()
if __name__ == '__main__':
main()
result
reference
[文档] docs.python.org/3/library/tkinter.html
resource
[文档] docs.python.org/3
[规范] www.python.org/dev/peps/pep-0008
[规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
[源码] www.python.org/downloads/source
[ PEP ] www.python.org/dev/peps
[平台] www.cnblogs.com
[平台] gitee.com
Python具有开源、跨平台、解释型和交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
GUI可以选择PyQt5、PySide2、wxPython、PyGObject、wxWidgets等进行创作。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。
-
Python tkinter 中 Button 关联函数的返回值获取小示例
2022-05-01 21:20:53在 tkinter 中 Button的 command 绑定函数时,发现获取不到 函数的返回值,这时可以怎么修改代码来实现呢? 可参考此方法:将函数需要返回的值 存储在一个字典中,以键值对的形式来存储和获取。 下面有一个用 ...在 tkinter 中 Button的 command 绑定函数时,发现获取不到 函数的返回值,这时可以怎么修改代码来实现呢?
可参考此方法:将函数需要返回的值 存储在一个字典中,以键值对的形式来存储和获取。
(补充,把结果值放在 List中,也是可以的)下面有一个用 Button 打开文件选择框从而获取文件路径的小示例:
1、运行这段代码:
2、点击按钮,选择文件,点击打卡,再关闭 窗口
3、关闭对话框后,可打印该文件路径,获取函数的返回值成功
本文的代码集:
from tkinter import * import tkinter.filedialog as fd result_dict = {'file_path':""} #用来存储函数中需返回的值 def getFileDir(): file_path = fd.askopenfilename(filetypes = [("xls","*.xls"),("All","*.*")]) result_dict['file_path'] = file_path #把结果放在字典里,作用相当于 return root = Tk() root.title('Demo') # 窗口标题 root.geometry('220x80') # 窗口大小 btn = Button(root,text ="选择文件",command = getFileDir) # 设置一个按钮 btn.pack() # 展示 root.mainloop() print(result_dict['file_path'])
本文仅供参考,希望对你有帮助!
若有什么其他方法,欢迎来评论区告知。
-
python:TKinter获取Text文本框的输入内容(python图形化界面)
2021-01-21 21:53:411. 示例代码: # -*- coding: utf-8 -*- import tkinter as tk window = tk.Tk() #建立窗口window window.title('示例1') #窗口名称 window.geometry("400x240") #...textExample.pack() #把Text放在window上面,显.1. 示例代码:
get_input.py
# -*- coding: utf-8 -*- import tkinter as tk #第1步,建立窗口window window = tk.Tk() #建立窗口window #第2步,给窗口起名称 window.title('示例1') #窗口名称 #第3步,设定窗口的大小(长*宽) window.geometry("400x240") #窗口大小(长*宽) #第4步,在图形化界面上设定一个文本框 textExample=tk.Text(window, height=10) #创建文本输入框 #第5步,安置文本框 textExample.pack() #把Text放在window上面,显示Text这个控件 #第6步,获取文本框输入 def getTextInput(): result=textExample.get("1.0","end") #获取文本输入框的内容 print(result) #输出结果 #Tkinter 文本框控件中第一个字符的位置是 1.0,可以用数字 1.0 或字符串"1.0"来表示。 #"end"表示它将读取直到文本框的结尾的输入。我们也可以在这里使用 tk.END 代替字符串"end"。 #第7步,在图形化界面上设定一个button按钮(#command绑定获取文本框内容的方法) btnRead=tk.Button(window, height=1, width=10, text="Read", command=getTextInput) #command绑定获取文本框内容的方法 #第8步,安置按钮 btnRead.pack() #显示按钮 #第9步, window.mainloop() #显示窗口
2. 运行结果:
参考:
1)图形化界面
https://blog.csdn.net/jacky_zhuyuanlu/article/details/77475635 Python图形用户界面-Tkinter
https://segmentfault.com/a/1190000021662869 Python图形界面GUI程序设计(画一个简单的窗口)
2)获取文本框的输入
https://www.delftstack.com/zh/howto/python-tkinter/how-to-get-the-input-from-tkinter-text-box/ 如何获取 Tkinter 文本框中的输入
https://blog.csdn.net/biubiuzzz/article/details/52144600 python TKinter获取文本框内容
https://blog.csdn.net/Vicky_P/article/details/85265952 Tkinter 学习笔记:如何用get()方法获取 Text() 和 Entry()文本框的内容
-
python中tkinter库中button如何通过command获取该button的名称
2020-09-23 14:09:54``` import tkinter import webbrowser def open_url(url): ...我想到的解决办法是,command=函数,这个函数可以获取目前button的名称,然后通过名称再去匹配相应的url,不过如何才能获取到此时的button的名称呢 -
如何在Tkinter中获取按钮的文本-问答-阿里云开发者社区-阿里云
2020-12-18 10:27:320) # Button(object1, text="Enter", width=15, height=2, command=quit).place(x=0, y=0) TopFrame = Frame(window1, bg = '#273c75', width = 950, height = 50).place(x=0,y=0) TopFrame2 = Frame(window1, bg = ... -
Python tkinter编程中,点击按钮怎么获取Text的内容
2019-02-26 17:54:13怎么获取 ``` from tkinter import * #from navigation_ui ... Button(top9,text="创建",command=self.creatBug).pack(side=LEFT) top9.pack(side=TOP,pady=10) def creatBug(self): #怎么获取 ``` -
解决Python Tkinter Button控件command传参问题
2020-12-11 11:36:09from tkinter import *import tkinter.messagebox as messageboxclass A:"""使用StringVar() 和 textvariable对Button进行绑定实现Button对数据进行操作解决Button传参问题StringVar()的数需要使用.get()获取值""... -
python:TKinter获取Text文本框的输入内容
2021-02-03 03:48:031. 示例代码:get_input.py# -*- coding: utf-8 -*-import tkinter as tkwindow = tk.Tk() #建立窗口windowwindow.title('示例1') #窗口名称window.geometry("400x240") #窗口大小(长*宽)textExample=tk.Text... -
Python中Button组件的属性及参数
2020-11-24 02:49:48Python中Button按钮组件常用的属性及参数设置温馨提示,文章篇幅较长,请耐心阅本篇文章中小编给大家介绍Button按钮组件的相关常用的属性以及参数的设置。一. 常用属性使用语法变量=Button(父容器(根窗口),参数=... -
python获取命令行输出结果
2020-12-28 18:47:57python获取命令行参数的方法(汇总) 介绍python获取命令行参数的方法:getopt模和argparse模块. python版本:2.7 一.getopt模块 主要用到了模块中的函数: options, args = getopt.g ... 【Python】Python获取命令行... -
关于Python Tkinter Button控件command传参问题的解决方式
2020-12-11 11:36:06环境:Ubuntu14、Python3.4、Pycharm2018一、使用command=lambda: 的形式传参代码如下from tkinter import *import tkinter.messagebox as messageboxdef createpage(master):master = Frame(root)master.pack()... -
Python中Tkinter解决button的command无返回值问题
2022-04-11 11:42:21Tkinter是什么 Tkinter是Python的标准GUI库。Python使用Tkinter可以快速地创建GUI应用程序。由于Tkinter属于Python标准库,就不需要使用pip安装,...问题:Button执行命令的时候,获取不到返回值怎么办? 场景:我需要 -
python如何获取tkinter组件的名称?比如按下多个Button中的一个,返回这个被按下的Button的text?
2021-03-17 02:29:54f=tk.Button(frm_4,text='0',bg='WhiteSmoke',font=('Arial','20')).place(x=90,y=355,width=90,height=80) #加减乘除 q=0 for p in ['+','-','*','/']: e=tk.Button(frm_3,text=p,bg='Gainsboro',font=('20')).... -
python获取所选单选按钮的文本/标签名称,而不仅仅是值
2021-02-03 05:00:00radio3.bind('', lambda event: self.show_radioname("radio3")) radio3.pack() submit_button = Button(self.master, text="print", command=self.show_var) submit_button.pack() self.master.mainloop() def show... -
Python tkinter之Text
2020-12-11 11:40:011、Text的基本属性#-*- encoding=utf-8 -*-importtkinterfrom tkinter import *if __name__ == '__main__':win= tkinter.Tk() #窗口win.title('南风丶轻语') #标题screenwidth = win.winfo_screenwidth() #屏幕宽度... -
Python+Selenium基础篇之13 -获取元素上面的文字
2022-03-31 11:21:31如何通过Selenium方法来获取某一个元素的text属性值。在很多自动化测试脚本中,需要多次获取元素的text值,拿过来进行对比和匹配。 判断登录键文字 代码如下: # coding=utf-8 import time from selenium import ... -
python基础知识--Button按钮,Label标签,Entry输入框,Text文本框参数属性详解
2020-09-29 20:20:15tkinter入门参数详解Label标签概念参数代码运行结果Button按钮概念参数代码结果展示Entry单行输入框和Text多行输入文本框概念参数代码结果展示 Label标签 概念 一个标签组件。主要用来实现显示功能,可以显示文字和... -
Python中按键来获取指定的值
2020-12-08 09:45:20Python中按键来获取值,相对来说要容易些,毕竟只需要dict[key]就可以找到,但里面同样有个问题,如果其中的键不存在的话,会抛出异常,如果不用try...except...等异常处理机制的话,程序就会中断!这里提供两种很... -
在PyQt Python ui文件中抓取所有QPushButton
2021-07-16 15:31:37I created a UI File using Qt Designer with lots of QPushButtons, and then I converted it into a python file using pyuic4.I want to add all the QPushButtons to a QButtonGroup.How do I iterate or grab a... -
Python Tkinter Button控件command传参问题解决方案
2018-08-17 18:43:29Python Button控件command传参问题解决方案 环境:Ubuntu14、Python3.4、Pycharm2018 一、使用command=lambda: 的形式传参 代码如下 from tkinter import * import tkinter.messagebox as messagebox def ... -
python – 在Kivy应用程序中获取textinput值
2020-12-08 14:00:46使用self.txt1,self.lbl1等.class MyApp(App):# layoutdef build(self):layout = BoxLayout(padding=10, orientation='vertical')btn1 = Button(text="OK")btn1.bind(on_press=self.buttonClicked)layout.add_widget... -
Python tkinter(一) 按钮(Button)组件的属性说明及示例
2021-01-28 19:12:05Python tkinter 按钮组件用于tkinter GUI里添加按钮,按钮可以添加文本和图像。当按钮按下时,可以执行指定的函数。使用语法:widget = Button( master, parameter=value, ... )master:按钮控件的父容器parameter:... -
从Python中的FileChooserButton获取文件夹路径
2021-02-11 01:08:03我正在尝试使用Python和Glade制作一个基本的文件传输GUI应用程序,在试图从文件选择器对话框获取路径时遇到了一点障碍。我尽了我所知所能找到的一切,但没有成功。在我的Python代码如下所示import datetimefrom ... -
Python中Button按钮组件常用的属性及参数设置
2020-05-15 18:33:35Python中Button按钮组件常用的属性及参数设置 本篇文章中小编给大家介绍Button按钮组件的相关常用的属性以及参数的设置。 1. 常用属性使用语法 变量=Button(父容器(根窗口),参数=参数值) 2. 常用参数说明 [ ]... -
Python之tkinter 功能按钮Button的基本应用
2020-03-08 12:01:09但是要在Button()内要增加image参数设置图像对象 例子:使用图像按钮 import tkinter def show(): label.config(text="Python!!!", bg="lightyellow", fg="red") root = tkinter.Tk() img = tkinter.PhotoImage(file...