简体   繁体   English

Python 解压文件

[英]Python Unzip file

Really inexperienced with python and still learning.对 python 真的很陌生,还在学习。 I am trying to unzip a file with the click of a button.我正在尝试通过单击按钮来解压缩文件。 When I run this program the GUI does not show the button.当我运行这个程序时,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()

There are a few changes you need to implement.您需要实施一些更改。
First import "tkinter", not "Tkinter".首先导入“tkinter”,而不是“Tkinter”。 Second, you need to change "Text" to be "text".其次,您需要将“文本”更改为“文本”。 Finally, you need to pack the button using "UnButton.pack()"最后,您需要使用“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()

Well, I rewrote it using the canvas method, as this will allow you to have more control over your application for positioning, add images, add shapes move shapes.好吧,我使用 canvas 方法重写了它,因为这将允许您更好地控制应用程序的定位、添加图像、添加形状移动形状。 Hope this helps, happy coding!!希望这会有所帮助,快乐的编码!

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()

Your Code is fixed.您的代码是固定的。 'UnButton.pack()' is added in code. 'UnButton.pack()' 被添加到代码中。

Follow this link to understand Widget.Pack() method按照此链接了解 Widget.Pack() 方法

https://www.tutorialspoint.com/python/tk_pack.htm 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM