【问题标题】:Python 2.7 GUI using Tkinter使用 Tkinter 的 Python 2.7 GUI
【发布时间】:2017-12-08 16:21:24
【问题描述】:

我对 Python 和 GUI 完全陌生。我正在尝试使用 Tkinter 创建一个简单的 GUI,它有两个文件浏览器选项和一个命令按钮。

当我尝试单击第一个浏览按钮并选择文件时,文件名显示在两个文本框中。同样,当我单击第二个按钮时,新文件名会显示在两个文本框中。请纠正我哪里出错了。我尝试了很多修复,但在静脉中。另外,请提出建议以改进我的编码,以减少这种简单 GUI 的 LOC。

Issue Sceenshot

import os
import Tkinter
import tkFileDialog
from tkFileDialog import askopenfilename
from Tkinter import *

file_path_1 = ''
file_path_2 = ''

#~~~~ FUNCTIONS~~~~

def open_rules_file():
  global file_path_1
  filename1 = askopenfilename()
  file_path_1 = os.path.abspath(filename1)
  print file_path_1
  entry1.delete(0, END)
  entry1.insert(0, file_path_1)

def open_src_file():
  global file_path_2
  filename2 = askopenfilename()
  file_path_2 = os.path.abspath(filename2)
  print file_path_2
  entry2.delete(0, END)
  entry2.insert(0, file_path_2)

#~~~~~~ GUI ~~~~~~~~

root = Tk()
root.title('MY GUI')
root.geometry("1000x300+250+100")

mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250)
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250)
f2.pack()
f3 = Frame(mf, width=600, height=250)
f3.pack()

file_path_1 = StringVar
file_path_2 = StringVar

Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e')
entry1 = Entry(f1, width=70, textvariable=file_path_1)
entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4)

Label(f2,text="Select COBOL Source   ").grid(row=2, column=0, sticky='e')
entry2 = Entry(f2, width=70, textvariable=file_path_2)
entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4)

Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4)
root.mainloop()

【问题讨论】:

  • 无法在我的机器上重现该问题。
  • 我已经更新了我的帖子以包含屏幕截图。请检查相同。
  • 我也无法重现该问题,对我来说很好。
  • 你为什么在你的代码中使用 StringVar ?

标签: python python-2.7 tkinter


【解决方案1】:

您只是将类分配给这些变量,而不是创建 StringVar 类的对象

import os
import Tkinter
import tkFileDialog
from tkFileDialog import askopenfilename
from Tkinter import *

file_path_1 = ''
file_path_2 = ''

#~~~~ FUNCTIONS~~~~

def open_rules_file():
  global file_path_1
  filename1 = askopenfilename()
  file_path_1 = os.path.abspath(filename1)
  print file_path_1
  entry1.delete(0, END)
  entry1.insert(0, file_path_1)

def open_src_file():
  global file_path_2
  filename2 = askopenfilename()
  file_path_2 = os.path.abspath(filename2)
  print file_path_2
  entry2.delete(0, END)
  entry2.insert(0, file_path_2)

#~~~~~~ GUI ~~~~~~~~

root = Tk()
root.title('MY GUI')
root.geometry("1000x300+250+100")

mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250)
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250)
f2.pack()
f3 = Frame(mf, width=600, height=250)
f3.pack()

##Instead of creating object of that class you have just assigned class to those variable
file_path_1 = StringVar()
file_path_2 = StringVar()

Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e')
entry1 = Entry(f1, width=70, textvariable=file_path_1)
entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4)

Label(f2,text="Select COBOL Source   ").grid(row=2, column=0, sticky='e')
entry2 = Entry(f2, width=70, textvariable=file_path_2)
entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4)

Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4)
root.mainloop()

【讨论】:

    【解决方案2】:

    由于您使用的是 Entry 小部件,因此您根本不需要 textvariable。只需删除它们并使用小部件自己的方法。

    def open_rules_file():
      filename1 = askopenfilename()
      print(filename1)
      entry1.delete(0, END)
      entry1.insert(0, filename1)
    
    def open_src_file():
      filename2 = askopenfilename()
      print(filename2)
      entry2.delete(0, END)
      entry2.insert(0, filename2)
    
    #~~~~~~ GUI ~~~~~~~~
    
    root = Tk()
    root.title('MY GUI')
    root.geometry("1000x300+250+100")
    
    mf = Frame(root)
    mf.pack()
    
    f1 = Frame(mf, width=600, height=250)
    f2 = Frame(mf, width=600, height=250)
    f3 = Frame(mf, width=600, height=250)
    
    f1.pack(fill=X)
    f2.pack()
    f3.pack()
    
    Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e')
    entry1 = Entry(f1, width=70)
    entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4)
    
    Label(f2,text="Select COBOL Source   ").grid(row=2, column=0, sticky='e')
    entry2 = Entry(f2, width=70)
    entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25)
    Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4)
    
    Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4)
    root.mainloop()
    

    但是如果你真的坚持使用它,你可以通过使用file_path_1.set("your value")来改变StringVar的值。

    您现在的用法 (file_path_1 = "value") 将 file_path_1 更改为纯字符串。

    def open_rules_file():
      var.set(askopenfilename())
    
    var = StringVar()
    entry1 = Entry(f1, width=70, textvariable = var)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多