【问题标题】:How to add frames in Python tkinter?如何在 Python tkinter 中添加帧?
【发布时间】:2019-02-24 09:54:55
【问题描述】:

在下面给出的代码中,我只想在选中“土壤”检查按钮时打开文件打开对话框,并对“天气”检查按钮重复相同的操作。

“土壤”检查按钮、“天气”检查按钮和“提交”按钮需要放置在 GUI 框架的左侧,滚动条应包含 2 个打开文件的详细信息(从“文件打开对话框”,“文本”格式)放置在 GUI 框架的右侧

from tkinter import *
from tkinter import Tk
from tkinter.filedialog import askopenfilename

win = Tk()

frame = Frame(win)
frame.pack()

rightframe = Frame(win)
rightframe.pack( side = RIGHT )

#frame_name = Frame(win)
#frame_address = Frame(win)

win.title("Spatialization of DSSAT model")

w = 800
h = 400

ws = win.winfo_screenwidth()
hs = win.winfo_screenheight()

x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

win.geometry('%dx%d+%d+%d' % (w, h, x, y))

def forCheckbutton1():
    filename1 = askopenfilename()
    print(filename1)

def forCheckbutton2():
    filename2 = askopenfilename()
    print(filename2)

def forMuButton1():
    win.destroy()

def var_states():
    print("soil: %d, \nweather:%d" % (MyVar1.get(), MyVar2.get()))

MyLabel1 = Label(frame, text="Select:")
MyLabel1.grid(row=0, column=0, sticky=W)


MyVar1 = IntVar()
MyVar2 = IntVar()

MyCheckbutton1 = Checkbutton(frame, text="soil", variable=MyVar1, command=forCheckbutton1)
MyCheckbutton1.grid(row=1, column=0, sticky=W)

MyCheckbutton2 = Checkbutton(frame, text="weather", variable=MyVar2, command=forCheckbutton2)
MyCheckbutton2.grid(row=2, column=0, sticky=W)


MyButton1 = Button(frame, text="Submit", width=10, command=forMuButton1)
MyButton1.grid(row=5, columnspan=3)

scrollbar = Scrollbar(rightframe)
scrollbar.pack( side = RIGHT, fill = Y )

myList = Listbox(rightframe, yscrollcommand = scrollbar.set )
forCheckbutton1()
forCheckbutton2()

myList.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = myList.yview )

win.mainloop()

【问题讨论】:

  • “细节”是什么意思?你是说文件内容吗?
  • 是的,是文本格式的文件内容
  • 就是@Heyran.rs文本格式的文件内容
  • 请检查我的回答。

标签: python tkinter


【解决方案1】:

仅在选中按钮时从主循环中删除 forCheckbutton1()forCheckbutton2() 以打开对话框。要将框架放置在左侧,请将side 设置为LEFT。对于滚动条中的文件内容,请在函数中打开文件。试试这个:

from tkinter import *
from tkinter import Tk
from tkinter.filedialog import askopenfilename

win = Tk()

frame = Frame(win)
frame.pack(side=LEFT)

rightframe = Frame(win)
rightframe.pack( side = RIGHT )

win.title("Spatialization of DSSAT model")

w = 800
h = 400

ws = win.winfo_screenwidth()
hs = win.winfo_screenheight()

x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

win.geometry('%dx%d+%d+%d' % (w, h, x, y))

def forCheckbutton1():
    filename1 = askopenfilename()

    with open(filename1) as f:
        for i in f:
            myList.insert(END, i)

    print(filename1)

def forCheckbutton2():
    filename2 = askopenfilename()

    with open(filename2) as f:
        for i in f:
            myList.insert(END, i)

    print(filename2)

def forMuButton1():
    win.destroy()

def var_states():
    print("soil: %d, \nweather:%d" % (MyVar1.get(), MyVar2.get()))

MyLabel1 = Label(frame, text="Select:")
MyLabel1.grid(row=0, column=0, sticky=W)


MyVar1 = IntVar()
MyVar2 = IntVar()

MyCheckbutton1 = Checkbutton(frame, text="soil", variable=MyVar1, command=forCheckbutton1)
MyCheckbutton1.grid(row=1, column=0, sticky=W)


MyCheckbutton2 = Checkbutton(frame, text="weather", variable=MyVar2, command=forCheckbutton2)
MyCheckbutton2.grid(row=2, column=0, sticky=W)


MyButton1 = Button(frame, text="Submit", width=10, command=forMuButton1)
MyButton1.grid(row=5, columnspan=3)

scrollbar = Scrollbar(rightframe)
scrollbar.pack( side = RIGHT, fill = Y )

myList = Listbox(rightframe, yscrollcommand = scrollbar.set )
myList.pack( side = LEFT, fill = BOTH )
scrollbar.config( command = myList.yview )

win.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2017-07-09
    相关资源
    最近更新 更多