简体   繁体   English

我在Tkinter上运行的代码有什么问题?

[英]What is the problem with my code run on Tkinter?

I'm a newbie to Python's Tkinter, and I'd like to create a program running on it. 我是Python Tkinter的新手,我想创建一个在其上运行的程序。 However, my code doesn't work correctly. 但是,我的代码无法正常工作。

from tkinter import *

def conv1(self):
    gbp0 = 174000000
    galleons0 = 34000872
    sickles0 = 14
    knuts0 = 7

    galleons1 = float(galleons0 + sickles0 / 17 + knuts0 / 29 / 17)
    fracture = float(gbp0 / galleons1)
    convert1 = Toplevel(root)
    convert1.title("Pounds Sterling (GBP) to Galleons, Sickles and Knuts Converter")

    label1_1 = Label(convert1, text="Type the amount of money in GBP that you would like to convert to Galleons, Sickles and Knuts and press Enter.")
    label1_2 = Label(convert1, text="1 Galleon = 5.12 GBP")
    label1_3 = Label(convert1, text='GBP:')

    label1_1.pack()
    label1_2.pack()
    label1_3.pack()

    usergbpvar = DoubleVar()
    usergbp = Entry(convert1, textvariable=usergbpvar)
    usergbp.pack()

    a = float(usergbpvar.get() / fracture)

    galleons = int(a // 1)
    a = (a % 1) * 17

    sickles = int(a // 1)
    a = (a % 1) * 29

    if (a % 1) == 0.5:
        knuts = int(round(a, 0))
        knuts += 1
    else:
        knuts = int(round(a, 0))

    galleons, sickles, knuts = str(galleons), str(sickles), str(knuts)

    label1_4 = Label(convert1, text=galleons)
    label1_5 = Label(convert1, text=sickles)
    label1_6 = Label(convert1, text=knuts)

    label1_4.pack()
    label1_5.pack()
    label1_6.pack()

    convert1.mainloop()

root = Tk()
btn1 = Button(root, text='GBP to Galleons, Sickles and Knuts', bg='#555', fg='#ccc', font='16')
btn1.pack()
btn1.bind('<Button-1>', conv1)
root.mainloop()

It's supposed to calculate three numbers out of the entered one, and to show them on the screen. 它应该从输入的数字中计算出三个数字,并将其显示在屏幕上。 However, when I run the program, after pressing the button I see that all the numbers are already there and they are 0. After I enter my number, nothing is changed. 但是,当我运行该程序时,按下按钮后,我看到所有数字都已经存在并且它们都是0。输入我的数字后,什么都没有改变。

Could you please tell me where the issue in my code is? 您能告诉我代码中的问题在哪里吗?

Problem/Question 1: 问题/问题1:

when I run the program, after pressing the button I see that all the numbers are already there and they are 0. 当我运行该程序时,按下按钮后,我看到所有数字都已经存在并且它们都是0。

When you call label1_4=Label(convert1, text=galleons) label1_4.pack() this tells tkinter to display the label immediately with the given value eg galleons for label1_4, which is 0 (same for the other labels). 当您调用label1_4=Label(convert1, text=galleons) label1_4.pack()这会告诉label1_4=Label(convert1, text=galleons) label1_4.pack()立即显示具有给定值的标签,例如,label1_4的加仑数为0(与其他标签相同)。 This isn't a problem and is expected as the value of the entry box is 0 to begin with. 这不是问题,可以预期,因为开始时输入框的值为0。

Problem/Question 2: 问题/问题2:

After I enter my number, nothing is changed. 输入号码后,什么都没有改变。

You don't actually tell the program to ever update the value of the labels. 您实际上并没有告诉程序曾经更新标签的值。 As TornaxO7 said, you need to bind the enter (return) key to call a function usergbp.bind("<Return>", calculation_function_here) 就像TornaxO7所说的那样,您需要绑定Enter(返回)键来调用函数usergbp.bind("<Return>", calculation_function_here)

I have edited your code to give an object oriented approach. 我已经编辑了您的代码以提供一种面向对象的方法。 I would suggest exploring this approach as you progress and perhaps want multiple windows. 我建议随着您的进步而探索这种方法,并且可能需要多个窗口。 Best way to structure a tkinter application? 构造tkinter应用程序的最佳方法?

from tkinter import *

class gui_window:

    def __init__(self, master):
        # setup gui
        self.master = master 
        self.master.wait_visibility() # attempt to fix traceback error, see Problem/question 3 below

        self.master.grab_set() # stops button1 creating another gui_window instance
        self.master.title('Pounds Sterling (GBP) to Galleons, Sickles and Knuts Converter')

        self.label1_1=Label(master, text="Type the amount of money in GBP that you would like to convert to Galleons, Sickles and Knuts and press Enter.")
        self.label1_1.pack()

        self.label1_2=Label(master, text="1 Galleon = 5.12 GBP")
        self.label1_2.pack()

        self.label1_3=Label(master, text='GBP:')
        self.label1_3.pack()

        self.usergbpvar=DoubleVar()
        self.usergbp=Entry(master, textvariable=self.usergbpvar)
        self.usergbp.bind("<Return>", self.calculate) # when user presses enter call the conversion function
        self.usergbp.pack()

        label1_4_1 = Label(self.master, text = 'Galleons:').pack(anchor = 'w')
        self.label1_4=Label(self.master, text='0', anchor = 'e')
        self.label1_4.pack()

        label1_5_1 = Label(self.master, text = 'Sickles:').pack(anchor = 'w')
        self.label1_5=Label(self.master, text='0', anchor = 'e')
        self.label1_5.pack()

        label1_6_1 = Label(self.master, text = 'Knuts:').pack(anchor = 'w')
        self.label1_6=Label(self.master, text='0')
        self.label1_6.pack()


        self.gbp0=174000000
        self.galleons0=34000872
        self.sickles0=14
        self.knuts0=7
        self.galleons1=float(self.galleons0+self.sickles0/17+self.knuts0/29/17)
        self.fracture=float(self.gbp0/self.galleons1)

    def calculate(self, event):
        # do calculation
        a=float(self.usergbpvar.get()/self.fracture)
        galleons=int(a//1)
        a=a%1
        a=a*17
        sickles=int(a//1)
        a=a%1
        a=a*29
        if a%1==0.5:
            knuts=int(round(a, 0))
            knuts=knuts+1
        else:
            knuts=int(round(a, 0))
        galleons=str(galleons)
        sickles=str(sickles)
        knuts=str(knuts)

        # update the labels to reflect the calculation
        self.label1_4.config(text=galleons)
        self.label1_5.config(text=sickles)
        self.label1_6.config(text=knuts)



def create_gui(self):
    # create a gui_window Toplevel instance 
    convert1=Toplevel() 
    gui_window(convert1)

root=Tk()
btn1=Button(root, text='GBP to Galleons, Sickles and Knuts', bg='#555', fg='#ccc', font='16')
btn1.pack()
btn1.bind('<Button-1>', create_gui) # call function to make next window
root.mainloop()

Problem/Question 3 from comments: I believe the error: tkinter.TclError: grab failed: window not viewable is dependant on your OS. 问题/来自评论的问题3:我相信错误: tkinter.TclError: grab failed: window not viewable取决于您的操作系统。 I am unable to reproduce the error on Mac OS but adding self.master.wait_visibility() (added into my code) may fix this: python tkinter treeview not allowing modal window with direct binding like on_rightclick 我无法在Mac OS上重现该错误,但添加self.master.wait_visibility() (添加到我的代码中)可能会解决此问题: python tkinter treeview不允许使用直接绑定(如on_rightclick)的模式窗口

I guess that you forgot to bind the Return -key. 我猜您忘了绑定Return键。
You should add convert1.bind("<Return>", *your function*) in your method. 您应该在您的方法中添加convert1.bind("<Return>", *your function*)
"your function" is the function which changes the numbers. “您的功能”是更改数字的功能。

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

相关问题 每当我运行我的程序时,以下 python tkinter 代码中的字体大小问题不会增加 - problem with font size in following python tkinter code whenever i run my program font size do not increase 不确定我的代码有什么问题(Tkinter BEGINNER) - Not sure what is wrong with my code (Tkinter BEGINNER) 为什么当我尝试运行此 tkinter python 代码时会出现问题 - Why is there a problem when i try to run this tkinter python code 尝试在 tkinter 中创建一个 9 x 9 数独网格,但每第九个正方形的填充 (padx) 已关闭。 我的代码有什么问题? - Trying to create a 9 x 9 sudoku grid in tkinter, but the padding (padx) for every ninth square is off. What is the problem in my code? 为什么我的 Tkinter 代码运行时间太长? - Why does my Tkinter code take too long to run? 为什么我的多帧 tkinter 代码甚至无法运行 - Why does my tkinter code with multiple frames not even run 我的代码中的 pandas 到 csv 有什么问题? - What is the problem with the pandas to csv in my code? 我的代码正在运行,但是没有输出,这是什么问题? - My code is working but with no outputs, what is the problem? 我的主要测试代码有什么问题? - What's the problem of my prime test code? projecteuler 问题 8 - 不确定我的代码有什么问题 - projecteuler problem 8 - unsure on what is wrong with my code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM