嗨,我有A str'对象没有属性' get' Python-tkinter中的问题

发布于 2025-01-27 18:59:13 字数 767 浏览 2 评论 0原文

它行不通 我希望它获得一个数字,然后单击这些按钮bellow,然后在消息框中获取结果,该怎么办?

from tkinter import *
from tkinter import messagebox 
from tkinter import ttk

win=Tk()
#here is my problem 
def household():
    global s
    global math
    math="multiply"
    x=int(E.get())
    s=(x/100)*500
    b="your bill is:"+str(s)
    messagebox.showinfo("result",b)
def commercial():
    global s
    x=int(E.get())
    if x<=4000000:
        s=(x/100)*750
household()            
commercial()
E=Entry(win,bg="#87CEFA")
b1=Button(win,text="Household",bg="#4169E1",command=household) 
b3=Button(win,text="commercial",bg="#4169E1",command=commercial)
E.place(x=100,y=85,width=100,height=20)
b1.place(x=100,y=165,width=100,height=30)
b3.place(x=150,y=165,width=100,height=30)

it doesn't work
i want it to get a number then click those button bellow and then get the result in the message box what should i do?!

from tkinter import *
from tkinter import messagebox 
from tkinter import ttk

win=Tk()
#here is my problem 
def household():
    global s
    global math
    math="multiply"
    x=int(E.get())
    s=(x/100)*500
    b="your bill is:"+str(s)
    messagebox.showinfo("result",b)
def commercial():
    global s
    x=int(E.get())
    if x<=4000000:
        s=(x/100)*750
household()            
commercial()
E=Entry(win,bg="#87CEFA")
b1=Button(win,text="Household",bg="#4169E1",command=household) 
b3=Button(win,text="commercial",bg="#4169E1",command=commercial)
E.place(x=100,y=85,width=100,height=20)
b1.place(x=100,y=165,width=100,height=30)
b3.place(x=150,y=165,width=100,height=30)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2025-02-03 18:59:13

您正在调用homepor()commercial()在定义E之前,因为您正在进行通配符导入(tkinter Import *)您正在从tkinter导入常量e,该>定义为字符串“ e”。

解决方案是:

  • 不要使用导入TKINTER的通配符导入TKINTER作为TK,然后使用tk。作为所有TKINTE对象的前缀(tk.entry) (...)tk.button(...)等)
  • 确保您的代码按正确的顺序运行。如果您具有取决于变量的函数,请确保在定义变量之前未调用这些函数。

You're calling household() and commercial() before you define E. Because you are doing a wildcard import (from tkinter import *) you're importing the constant E from tkinter, which is defined as the string "e".

The solution is:

  • don't do wildcard imports import tkinter with import tkinter as tk and then use tk. as a prefix for all tkinter objects (tk.Entry(...), tk.Button(...), etc)
  • Make sure your code runs in the correct order. If you have functions that depend on a variable, make sure those functions aren't called before the variable is defined.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文