我試圖在tkinter中編寫一個應用程序,該應用程序將從您從下拉菜單中選擇的文件加載隨機行,並在文本窗口中顯示選定的行。tkinter變量下拉選擇空
好像在insert_text
變量「VAR」不返回所選擇的「選項」,但造成了下面的錯誤,而「空」字符串:
"File not found error" (FileNotFoundError: [Errno2] No such file or directory: '').
請幫幫忙!
#!/usr/bin/env python
# Python 3
import tkinter
from tkinter import ttk
import random
class Application:
def __init__(self, root):
self.root = root
self.root.title('Random Stuff')
ttk.Frame(self.root, width=450, height=185).pack()
self.init_widgets()
var = tkinter.StringVar(root)
script = var.get()
choices = ['option1', 'option2', 'option3']
option = tkinter.OptionMenu(root, var, *choices)
option.pack(side='right', padx=10, pady=10)
def init_widgets(self):
ttk.Button(self.root, command=self.insert_txt, text='Button', width='10').place(x=10, y=10)
self.txt = tkinter.Text(self.root, width='45', height='5')
self.txt.place(x=10, y=50)
def insert_txt(self):
var = tkinter.StringVar(root)
name = var.get()
line = random.choice(open(str(name)).readlines())
self.txt.insert(tkinter.INSERT, line)
if __name__ == '__main__':
root = tkinter.Tk()
Application(root)
root.mainloop()
感謝您編輯我的問題! :)我看到它看起來很奇怪,但是當我試圖編輯它時,就像我第一次輸入它一樣。 : -/ – moupy 2014-09-02 21:27:18