对蟒蛇缺乏经验,而且还在学习。我正在尝试解压缩一个文件,点击一个按钮。当我运行这个程序时,GUI不显示按钮。
from zipfile import ZipFile
import Tkinter
top = Tkinter.Tk()
top.title("NAME of Program")
def Unzip():
with ZipFile ('Test.zip', 'r') as zipObj:
zipObj.extractall()
UnButton = Tkinter.Button(Text="Unzip", command = Unzip)
top.mainloop()发布于 2021-05-06 22:47:53
您需要实现一些更改。
第一次进口"tkinter“而不是"Tkinter”。第二,你需要将“文本”改为“文本”。最后,您需要使用"UnButton.pack()“来打包按钮
from zipfile import ZipFile
import tkinter # Changed
top = tkinter.Tk() # Changed
top.title("NAME of Program")
def Unzip():
with ZipFile ('Test.zip', 'r') as zipObj: # Adding a Tab (4 spaces)
zipObj.extractall()
UnButton = tkinter.Button(text="Unzip", command = Unzip) # Changed
UnButton.pack() # Added
top.mainloop()发布于 2021-05-06 23:16:23
嗯,我用画布方法重写了它,因为这将允许您更好地控制应用程序的定位、添加图像、添加形状移动形状。希望这有帮助,快乐编码!
from zipfile import ZipFile
from tkinter import *
root = Tk()
root.title("NAME of Program")
canvas = Canvas(width=500, height=500)
canvas.pack(fill="both", expand=True)
def unzip():
with ZipFile('Test.zip', 'r') as zipObj:
zipObj.extractall()
UnButton = Button(text="Unzip", command=unzip)
# First value is x coordinate, horizontal axis, second value is y coordinate vertical axis, window should equal the Variable of the
# tkinter widget
# you can create as many windows for canvas as you need for each element
canvas.create_window(235, 150, anchor="nw", window=UnButton)
mainloop()发布于 2021-05-06 23:18:37
你的密码是固定的。代码中添加了'UnButton.pack()‘。
按照这个链接来理解Widget.Pack()方法
https://www.tutorialspoint.com/python/tk_pack.htm
from zipfile import ZipFile
import tkinter
top = tkinter.Tk()
top.title("NAME of Program")
def Unzip():
with ZipFile ('Test.zip', 'r') as zipObj:
zipObj.extractall()
UnButton = tkinter.Button(text="Unzip", command = Unzip)
UnButton.pack()
top.mainloop()https://stackoverflow.com/questions/67426822
复制相似问题