GUI python的Tkinter代码 [英] Tkinter Code for GUI python

查看:25
本文介绍了GUI python的Tkinter代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为我的 python 代码制作一个图形窗口,但我不知道如何添加按钮,我查看了网站,但我的版本 (3.2.3) 中没有我有一些现在代码,这有什么问题吗?

I have been trying to make a graphics window for my python code but I don't know how to add buttons and I have looked at websites and it hasn't been for my edition (3.2.3) I have got some code now, is there anything wrong with this?

import tkinter
import tkinter as tk
root = tk.Tk()
root.title("Adventure game")
root.geometry("1820x1000")
root.mainloop()
class Window(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.geometry("600x400+30+30")
        wButton = Button(self, text='start', command = self.OnButtonClick())
        wButton.pack()

        def OnButtonClick(self):
            top = Toplevel()
            top.title("title")
            top.geometry("300x150+30+30")
            topButton = Button(top, text="OPTION_1", command = self.OnButtonPress())
            topButton = Button(top, text="OPTION_2", command = self.OnButtonPress())
            topButton.pack()

            def OnButtonPress(self):
                top = Toplevel()
                top.title("title")
                top.geometry("300x150+30+30")
                topButton = Button(top, text="OPTION_1", command = self.destroy)
                bottomButton = Button(top, text="OPTION_2", command = self.destroy)
                topButton.pack()

推荐答案

回答您发布的问题这有什么问题吗?":

Answering your posted question "is there anything wrong with this?":

长话短说是的,有.

首先,您的缩进希望看起来不像在您的真实代码中,而只是在您的帖子中.

First, your indentation is hopefully not like it looks in your real code but in your post only.

其次,您导入和使用 tkinter 库的方式.

Second, the way you import and use tkinter libraries.

  • import tkinter VS import tkinter as tk:

请务必使用其中之一.不能两者兼而有之.

please do use either one of them. not both.

类窗口(Tk):

这应该会产生错误,因为您无法根据您的导入访问 Tk.class Window(tkinter.Tk):class Window(tk.Tk): 基于你之前的决定.

this should give an error as you have no access to Tk based on your imports. Either class Window(tkinter.Tk): or class Window(tk.Tk): based on your previous decision.

相同的操作适用于您在代码中使用的所有 tkinter 引用.

the same actions apply to all tkinter references you use inside your code.

有没有其他方法可以解决这个问题?- 是的:

from tkinter import * 可以让您直接访问名称以使 class Window(Tk): 工作.

from tkinter import * would give you access to the names directly to make class Window(Tk): work.

你为什么不早点告诉我?

一开始可以从库中导入所有内容.应该这样做吗?恕我直言,应该尽可能避免使用太多依赖项.为什么?因为它使代码更轻量级和可转移.为什么要导入代码中没有用到的东西?

One can import all from a library at the beginning. Should one do that? IMHO one should avoid using too many dependencies whenever possible. Why? Because it makes code more lightweight and transferrable. Why should I import things that are not used in my code?

仔细阅读 tkinter 上的文档.就像 martineau 提到的那样,有成千上万个示例代码库",所有可能格式的文档(书籍、电子书、论文、在线教程......)

Read the documentation on tkinter carefully. There are , like martineau mentioned , thousands over thousands of example "code bases" around, documentation in all possible formats (books, ebooks, papers, online tutorials ... )

阅读 python 文档.至少当你在某些部分遇到困难时.

Read the python documentation. At least when you encounter difficulties with certain parts.

在发布新问题之前先查看问答.针对同一目标的问题有几十个,仅代表 SO.

Have a look at Q&A before posting new questions. Questions aiming the same goal are available in dozens speaking only for SO.

一些例子:

所有这些问题都提供了您可以使用的示例,即使他们没有考虑相同的问题.这些问题提供了可用于您自己的代码示例.

All these Questions give examples you can use even if they do not have the same question in mind. These questions provide code examples you can use for your own good.

这篇关于GUI python的Tkinter代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆