我试图使其成为当用户点击按钮时,它变为"X"或"0"(取决于他们的团队).我怎样才能使按钮上的文字更新?到目前为止,我最好的想法是删除按钮然后再次打印它们,但这只删除了一个按钮.这是我到目前为止所拥有的:
from tkinter import *
BoardValue = ["-","-","-","-","-","-","-","-","-"]
window = Tk()
window.title("Noughts And Crosses")
window.geometry("10x200")
v = StringVar()
Label(window, textvariable=v,pady=10).pack()
v.set("Noughts And Crosses")
def DrawBoard():
for i, b in enumerate(BoardValue):
global btn
if i%3 == 0:
row_frame = Frame(window)
row_frame.pack(side="top")
btn = Button(row_frame, text=b, relief=GROOVE, width=2, command = lambda: PlayMove())
btn.pack(side="left")
def PlayMove():
BoardValue[0] = "X"
btn.destroy()
DrawBoard()
DrawBoard()
window.mainloop()
Run Code Online (Sandbox Code Playgroud) 我不明白我在这段代码中做错了什么(我正在启动GUI)
from Tkinter import *
def dn():
print("Do nothing")
root = Tk()
menu = Menu(root)
root.config(menu=menu)
subm= Menu(menu)
menu.add_cascade(label="File", menu=subm)
subm.add_command(label="New...", command=dn)
subm.add_command(label="Save!", command=dn)
subm.add_separator()
subm.add_command(label="Exit!", command=dn)
editmenu = Menu(menu)
menu.add_cascade(label="Edit", menu=editmenu)
editmenu.add_command(label="Undo", command=dn
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
它告诉我根源
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
是无效的,我不明白为什么?
提前感谢您的帮助
我想保存并加载我的GUI.
我制作了一个GUI,当我点击保存按钮时我想要它.
它应该在一些blob数据中保存GUI,当我点击加载按钮然后它将再次加载相同的GUI.
我的GUI有各种文本小部件,下拉选项菜单.我是python的新手,所以有人可以帮我解决这个问题吗?
我也试过泡菜模块.
我定义了一个类和该类中的变量。我在所有变量名之前都添加了self.varname。我不知道出什么问题了,但是在尝试运行脚本时,我不断收到“未定义全局名称'IntVar'
这是代码的一部分:
import tkinter as tk
from tkinter import Frame, Button, Label
import time
import random
class fragal(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.mymaster = master
self.primes = []
self.cntr1a = 0
self.cntr1b = 0
self.cntr2a = 0
self.cntr2b = 0
self.count = IntVar()
self.count.set(0)
self.time_remaining = IntVar()
self.time_remaining.set(0)
self.gamearray = []
self.equation = StringVar()
self.equation.set("")
Run Code Online (Sandbox Code Playgroud)
请帮忙!我的程序有多个版本,这个版本一直有效,直到我开始更改main()循环为止。
我是Python的新手,但了解基础知识.我一直在看几个教程Tkinter,但即使从一开始我输入:
from Tkinter import *
root = Tk()
Run Code Online (Sandbox Code Playgroud)
它给了我错误:
>Traceback (most recent call last):
File "/Users/$Name/Desktop/PycharmProjects/untitled/Tkinter.py", line 1, in <module>
from Tkinter import *
File "/Users/$Name/Desktop/PycharmProjects/untitled/Tkinter.py", line 3, in <module>
root = Tk()
NameError: name 'Tk' is not defined
Run Code Online (Sandbox Code Playgroud)
我尝试过不同的东西,而且我已经习惯了IDLE,但它仍然不起作用.我也在使用Mac OS X El Capitan,如果这很重要的话.
我有个问题.我认为一个类可以基于一个对象或以前定义类.当我将其更改为类Application(对象)时:它不起作用.你能告诉我为什么它不起作用,为什么下面的代码有效或为什么类应用程序(框架)有效?Frame不是先前定义的对象而不是对象.对不起我的英语不好.这是我的代码:
# Lazy Buttons
# Demonstrates using a class with tkinter
from tkinter import *
class Application(Frame): #
""" A GUI application with three buttons. """
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create three buttons that do nothing. """
# create the first Button
self.bttn1 = Button(self, text= "I do nothing.")
self.bttn1.grid()
# create second button
self.bttn2 = Button(self)
self.bttn2.grid()
self.bttn2.configure(text="Me too!")
# create third button
self.bttn3 = Button(self)
self.bttn3.grid()
self.bttn3["text"] = "Same here!"
# main …Run Code Online (Sandbox Code Playgroud) 我正在尝试用python中的GUI来控制我的机器人汽车.我的问题是我如何做一个确定按下按钮的功能.我想在按下按钮时移动汽车,并在释放按钮时停止汽车.
from Tkinter import *
hold_down = False
root = Tk()
def button_hold(event):
hold_down=true
while hold_down== True:
print('test statement')
hold_down = root.bind('<ButtonRelease-1>',stop_motor)
def stop_motor(event):
hold_down= False
print('button released')
button = Button(root, text ="forward")
button.pack(side=LEFT)
root.bind('<Button-1>',button_forward)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
我试图模拟我在这个答案中找到的东西
我尝试在while一个布尔循环中做到这一点.当用户按下按钮时,布尔值变为True,代码进入while循环.当用户释放按钮时,布尔值会更改为,False并且代码将从循环中退出,但在此代码中,无论是否释放按钮,布尔值始终为true.
编辑:我希望在条件发生之前调用函数.要调用的函数是hold_down(),要检查的条件是按钮被释放.
更新:我找到了一种方法让它工作.
我正在尝试在Tkinter应用程序中打开一个新的终端窗口。我已经使用过os.system(“ / bin / bash”),但是它仅在当前bash中有效。这将停止运行代码并导致应用程序冻结。我要打开新的终端。怎么做?像程序这样的gedit可以正常运行,而不会干扰当前的应用程序。
def openterm():
os.system("/bin/bash")
def opengedit():
os.system("/usr/bin/gedit")
menu = tk.Menu(root)
root.config(menu=menu)
subMenu = tk.Menu(menu)
menu.add_cascade(label="Tools", menu=subMenu)
subMenu.add_command(label="Open Terminal", command=openterm)
subMenu.add_command(label="Open Gedit", command=opengedit)
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试通过使用返回键绑定来更改tkinter标签小部件中的图像。按下返回键后,我希望图像更改为“ im2”,然后等待2秒钟,然后再次更改为“ im3”。到目前为止,我使用的代码是:
window = tk.Tk()
window.title("Testwindow")
window.geometry("800x800")
window.configure(background='grey')
# images
im1_path = "im1.gif"
im2_path = "im2.gif"
im3_path = "im3.gif"
im1 = ImageTk.PhotoImage(Image.open(im1_path))
im2 = ImageTk.PhotoImage(Image.open(im2_path))
im3 = ImageTk.PhotoImage(Image.open(im3_path))
panel = tk.Label(window, image = im1)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def callback(e):
panel.configure(image = im2)
panel.image = im2
time.sleep(2)
panel.configure(image = im3)
panel.image = im3
window.bind("<Return>", callback)
window.mainloop()
Run Code Online (Sandbox Code Playgroud)
但是,不是两次更改图像,而是在按回车键2秒后仅将其更改为“ im3”一次,因此不显示第一个更改。有人知道为什么吗?
我一直在研究Chris Meyers Tkinter教程并注意到一个奇怪的例外.当我运行程序并"加载"列表框中的一个项目并更新信息时,如果我将光标留在框中的任何位置,我会收到以下错误:
IndexError:元组索引超出范围
但是,如果我在框外单击并单击更新按钮,它可以正常工作.关于为什么会发生这种情况以及如何防范这种情况的任何想法?这是完整的程序:
from Tkinter import *
from phones import *
def whichSelected () :
print "At %s of %d" % (select.curselection(), len(phonelist))
return int(select.curselection()[0])
def addEntry () :
phonelist.append ([nameVar.get(), phoneVar.get()])
setSelect ()
def updateEntry() :
phonelist[whichSelected()] = [nameVar.get(), phoneVar.get()]
setSelect ()
def deleteEntry() :
del phonelist[whichSelected()]
setSelect ()
def loadEntry () :
name, phone = phonelist[whichSelected()]
nameVar.set(name)
phoneVar.set(phone)
def makeWindow () :
global nameVar, phoneVar, select
win = Tk()
frame1 = Frame(win)
frame1.pack()
Label(frame1, …Run Code Online (Sandbox Code Playgroud) tkinter ×10
python ×7
python-3.x ×3
bash ×1
linux ×1
pickle ×1
python-2.7 ×1
robotics ×1
textbox ×1
tk-toolkit ×1