python 如何调用选项/点击在另一个文件在tkinter?

kkbh8khc  于 2023-01-29  发布在  Python
关注(0)|答案(1)|浏览(152)

最近我正在学习tkinter,我想把1个代码分成2个代码,但是我总是发现错误

clicked = StringVar()
NameError: name 'StringVar' is not defined

或者我们如何在按下按钮后发送/捕获另一个文件?
home.py

from tkinter import *
from tkinter import ttk
from w2 import *
options =[
    "cone",
    "Ball",
    "Cube",
    "Beam"
    ]

clicked = StringVar()
clicked.set(options[0])
Label(win, text="Enter Number", font=('Calibri 10'), bg="yellow", fg="black").place(x=315, y=5)
drop = ttk.Combobox(win, width = 27, textvariable = clicked, value=options)
drop.place(x=265, y=35)

w2.py

def show(): 
    for i in options:
        if  clicked.get() == 'cone':
                myButton1['state'] = DISABLED
                mylabel = Label(win, text="Volume Cone",bg="yellow", fg="black")
                mylabel.place(x=315, y=125)
                mylabels = Label(win, text="r : ",bg="yellow", fg="black")
                mylabels.place(x=180, y=155)
                mylabelq = Label(win, text="h : ",bg="yellow", fg="black")
                mylabelq.place(x=350, y=155)
                a=Entry(win, width=21)
                a.place(x=200, y=155)
                b=Entry(win, width=22)
                b.place(x=375, y=155)
                myDel2 = Button(win, text="CLEAR VOLUME CONE", command=myDel)
                myDel2.place(x=200, y=178)
                my_label2 = Label(height=89,width=259,image=my_img)
                my_label2.place(x=225, y=238)
                myButton23 = Button(win,width=18, text="calculate", command=cal_cone , bg="red", fg="white")
                myButton23.place(x=375, y=178)
                wlabel = Label(win, text="")
                break

这是完整的代码
https://github.com/IMVERBILIUS/SIMPLE_VOLUME_CONE-BALL_GUI/blob/master/home.py

k4emjkb1

k4emjkb11#

  • home.py中.创建了一个名为show_w2()的函数
  • 添加按钮并为command=show_w2添加参数
  • w2.py中,添加tkinter
  • 注解掉第6 - 8行。
  • w2.py中.创建了一个名为run()的函数

代码:

from tkinter import *
from tkinter import ttk
 

win = Tk()
win.geometry('700x400')

options =[
    "cone",
    "Ball",
    "Cube",
    "Beam"
    ]

def show_w2():
    import w2
    w2.run()
    
clicked = StringVar()
clicked.set(options[0])
Label(win, text="Enter Number", font=('Calibri 10'), bg="yellow", fg="black").place(x=315, y=5)

drop = ttk.Combobox(win, width = 27, textvariable=clicked, value=options)
drop.place(x=265, y=35)

show_button = Button(win, text="Show", bg="red", fg="white", command=show_w2)
show_button.place(x=265, y=61)
 
win.mainloop()

w2.py.
代码:

from tkinter import *

 
def show():
     
    #for i in options:
    #if  clicked.get() == 'cone':
    #myButton1['state'] = DISABLED
    mylabel = Label( text="Volume Cone",bg="yellow", fg="black")
    mylabel.place(x=315, y=125)
    mylabels = Label( text="r : ",bg="yellow", fg="black")
    mylabels.place(x=180, y=155)
    mylabelq = Label( text="h : ",bg="yellow", fg="black")
    mylabelq.place(x=350, y=155)
    a = Entry(width=21)
    a.place(x=200, y=155)
    b = Entry(width=22)
    b.place(x=375, y=155)
    myDel2 = Button( text="CLEAR VOLUME CONE")
    myDel2.place(x=200, y=178)
    my_label2 = Label(height=89,width=259)
    my_label2.place(x=225, y=238)
    myButton23 = Button(width=18, text="calculate", bg="red", fg="white")
    myButton23.place(x=375, y=178)
    wlabel = Label( text="")
        
              
def run():
   show()

截图:

点击显示按钮后截图:

相关问题