#导入相关库 import tkinter as tk from tkinter import ttk #定义主窗口 root = tk.Tk() root.title("物流中心自动引导小车监控管理系统-用户权限管理") #定义标签控件 label = ttk.Label(root, text="请选择您的角色和权限:")label.grid(column=0,row=0,padx=10,pady=10) #定义下拉菜单控件 role_var = tk.Stringvar(role_var.set("管理员") role_options =["管理员","普通用户","访客"] role_menu = ttk.optionMenu(root, role_var,*role_options)role_menu.grid(column=0,row=1,padx=10,pady=10) permission_var = tk.Stringvar()
时间: 2024-03-10 08:49:25 浏览: 106
这段代码中,第6行缺少一个括号,应该是:
```
role_var = tk.StringVar(role_var.set("管理员"))
```
同样地,第9行也缺少一个括号,应该是:
```
role_menu = ttk.OptionMenu(root, role_var, *role_options)
```
请在代码中进行修改,这样就能够解决语法错误了。
相关问题
import tkinter as tk from tkinter import ttk from forex_python.converter import CurrencyRates # 创建货币转换器对象 c = CurrencyRates() # 创建主窗口 root = tk.Tk() root.title('货币转换器') # 创建控件 amount_label = ttk.Label(root, text='金额:') amount_entry = ttk.Entry(root) from_currency_label = ttk.Label(root, text='从货币类型:') from_currency_combo = ttk.Combobox(root, values=['USD', 'EUR', 'JPY', 'GBP']) to_currency_label = ttk.Label(root, text='到货币类型:') to_currency_combo = ttk.Combobox(root, values=['USD', 'EUR', 'JPY', 'GBP']) convert_button = ttk.Button(root, text='转换') result_label = ttk.Label(root, text='转换结果:') result_value = ttk.Label(root, text='') # 控件布局 amount_label.grid(row=0, column=0, padx=5, pady=5, sticky='w') amount_entry.grid(row=0, column=1, padx=5, pady=5) from_currency_label.grid(row=1, column=0, padx=5, pady=5, sticky='w') from_currency_combo.grid(row=1, column=1, padx=5, pady=5) to_currency_label.grid(row=2, column=0, padx=5, pady=5, sticky='w') to_currency_combo.grid(row=2, column=1, padx=5, pady=5) convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5) result_label.grid(row=4, column=0, padx=5, pady=5, sticky='w') result_value.grid(row=4, column=1, padx=5, pady=5) # 定义转换函数 def convert(): amount = float(amount_entry.get()) from_currency = from_currency_combo.get() to_currency = to_currency_combo.get() result = c.convert(from_currency, to_currency, amount) result_value.config(text=result) # 绑定事件 convert_button.config(command=convert) # 运行主循环 tk.mainloop() 优化此程序使窗口能运行
以下是优化后的代码:
```python
import tkinter as tk
from tkinter import ttk
from forex_python.converter import CurrencyRates
class CurrencyConverter:
def __init__(self, root):
self.root = root
self.root.title('货币转换器')
self.c = CurrencyRates()
self.create_widgets()
def create_widgets(self):
# 创建控件
amount_label = ttk.Label(self.root, text='金额:')
self.amount_entry = ttk.Entry(self.root)
from_currency_label = ttk.Label(self.root, text='从货币类型:')
self.from_currency_combo = ttk.Combobox(self.root, values=['USD', 'EUR', 'JPY', 'GBP'])
to_currency_label = ttk.Label(self.root, text='到货币类型:')
self.to_currency_combo = ttk.Combobox(self.root, values=['USD', 'EUR', 'JPY', 'GBP'])
convert_button = ttk.Button(self.root, text='转换', command=self.convert)
result_label = ttk.Label(self.root, text='转换结果:')
self.result_value = ttk.Label(self.root, text='')
# 控件布局
amount_label.grid(row=0, column=0, padx=5, pady=5, sticky='w')
self.amount_entry.grid(row=0, column=1, padx=5, pady=5)
from_currency_label.grid(row=1, column=0, padx=5, pady=5, sticky='w')
self.from_currency_combo.grid(row=1, column=1, padx=5, pady=5)
to_currency_label.grid(row=2, column=0, padx=5, pady=5, sticky='w')
self.to_currency_combo.grid(row=2, column=1, padx=5, pady=5)
convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
result_label.grid(row=4, column=0, padx=5, pady=5, sticky='w')
self.result_value.grid(row=4, column=1, padx=5, pady=5)
def convert(self):
amount = float(self.amount_entry.get())
from_currency = self.from_currency_combo.get()
to_currency = self.to_currency_combo.get()
result = self.c.convert(from_currency, to_currency, amount)
self.result_value.config(text=result)
if __name__ == '__main__':
# 创建主窗口并运行
root = tk.Tk()
currency_converter = CurrencyConverter(root)
root.mainloop()
```
这里将原来的代码封装在一个类 `CurrencyConverter` 中,使得代码更加模块化和易读。将 `convert()` 方法绑定到转换按钮上,实现转换功能。同时将控件中需要动态修改的部分(如输入框、下拉框、结果标签)设为实例属性,方便在 `convert()` 方法中调用和修改。最后在 `__main__` 中创建主窗口并运行。
下列代码中,文本框能显示,其它控件如notebook都不能显示。请给出修改后的代码。import tkinter as tk import tkinter.font as tkFont from tkinter.scrolledtext import ScrolledText # 导入ScrolledText from tkinter.filedialog import * from tkinter.ttk import * from tkinter import * import tkinter.messagebox from pystray import MenuItem, Menu from PIL import Image import pandas as pd class tkinterGUI(): root = None # 定义为类属性,可以在类的多个实例中共享 def __init__(self, geometry): pass def test(self): pass def create_root_win(self): self.root, self.文本框_主消息 = self.create_toplevel_win(True, "软件标题", "430x670", self.test, False, False) self.root.mainloop() # 在 create_root_win 方法中调用 mainloop 方法,显示窗口 def root_win_add1(self): if self.root is None: self.create_root_win() self.文本框_主消息.insert("1.0","efdssfdadsfasf") # 主内容区域 notebook = Notebook(self.root) notebook.pack(fill=tk.BOTH, expand=True) def create_toplevel_win(self,if_root,title,size,close_cmd,textbox_n,if_resize_width=True,if_resize_heigh=True): if if_root: mygui=tk.Tk() else: mygui=tk.Toplevel(self.root) 窗口win启动 = True mygui.title = title mygui.protocol('WM_DELETE_WINDOW', close_cmd) # 把点击x关闭窗口变成不要关闭并最小化到托盘 # 设置大小 居中展示 #win.bind("<Configure>", lambda root:win_mouse_release(root)) mygui.resizable(width=if_resize_width, height=if_resize_heigh) mygui.wm_attributes('-topmost', 1) #mygui.geometry(size+ "+" + str(self.root.winfo_x() + self.root.winfo_width()) + "+" + str(self.root.winfo_y())) mygui.geometry(size) tbox = ScrolledText(mygui) #self.eval("文本框"+title) = ScrolledText(self.win) tbox.place(relx=0.01, rely=0.18, relwidth=0.99, relheight=0.8) mygui.mainloop() return mygui,tbox # a,b=400,650 def show_msg_in_toplevel(self): self.win_msg,self.win_msg_tb= self.create_toplevel_win(self.root,"实时解盘","350x670",self.隐藏到任务栏,False,False) if __name__=="__main__": root=tkinterGUI("360x670") root.root_win_add1()
可能是因为notebook控件没有被正确地添加到窗口中。以下是修改后的代码:
import tkinter as tk
import tkinter.font as tkFont
from tkinter.scrolledtext import ScrolledText
root = tk.Tk()
# 创建notebook控件
notebook = tk.ttk.Notebook(root)
# 创建文本框控件
text = ScrolledText(root)
# 将文本框添加到notebook中
notebook.add(text, text="文本框")
# 将notebook添加到窗口中
notebook.pack(expand=True, fill="both")
root.mainloop()
阅读全文