如何使用python和tkinter在笔记本小部件中插入滚动条和表单?

2024-10-01 11:33:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python非常陌生,目前我正试图使用tkinter设计一个表单。我被困在试图在笔记本中插入滚动条和表单,因为我还没有找到我的问题的答案,而且很简单“如何在笔记本tkinter小部件中插入滚动条和表单?”。。。正如你所看到的,对你来说很简单,但对我这样的新手来说却不是!在

不过,这是我目前为止所做的,幸运的是它确实显示了滚动条,但是当我尝试将表单插入笔记本时,它崩溃了!在

注意:我的python版本是python2.7.3,EPD_free 7.3-2(32位)

import Tkinter
from Tkinter import * 
from ttk import *   
import tkMessageBox 
import ttk  
import Tkinter as tk 

root = Tk()
root.title("Model_A")
root.resizable(0,0)

# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

#Child Frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)
ContainerThree = Frame(notebook)
ContainerThree.pack(fill=BOTH, expand=True)
ContainerFour = Tkinter.Frame(notebook)
ContainerFour.pack(fill=BOTH, expand=True)

#Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')
notebook.add(ContainerThree, text='Mode C')
notebook.add(ContainerFour, text='Mode D')

canvas = Canvas(ContainerOne, width=200, height=400)
scroll = Scrollbar(ContainerOne, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerTwo, width=200, height=400)
scroll = Scrollbar(ContainerTwo, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

canvas = Canvas(ContainerThree, width=200, height=400)
scroll = Scrollbar(ContainerThree, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas = Canvas(ContainerFour, width=200, height=400)
scroll = Scrollbar(ContainerFour, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
frame = Frame(canvas, width=200, height=1000)
canvas.create_window(100, 500, window=frame)

frameOne = None  

def defocus(event):
    event.widget.master.focus_set()    
    if __name__ == '__main__':
        ContainerOne= Tkinter.Label(notebook, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
        frameOne.grid(row=2, columnspan=7, sticky='W', \
                 padx=5, pady=5, ipadx=5, ipady=5)

#Component Selection
componentComb= ttk.Combobox(ContainerOne, width="19")
componentComb = Combobox(ContainerOne, state="readonly", values=("A", "B", "C"))
componentComb.grid(column=4, row=4, columnspan="5", sticky="nswe")
componentComb.set("Main Selection")

root.mainloop()

Tags: importtruetkinterrootwidthfillsidepack
2条回答

如果您看一下options of the Notebook widget,就会发现yview和{}都不存在。此外,框架小部件也不可滚动。在

您可以做的是在frameOne内创建一个带有滚动条的画布小部件,然后使用^{}向画布添加一个框架。在

root = Tk()
root.resizable(0,0)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None
container = Frame(notebook)
container.pack(fill=BOTH, expand=True)
notebook.add(container, text='Mode A')

canvas = Canvas(container, width=200, height=400)
scroll = Scrollbar(container, command=canvas.yview)
canvas.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frame = Frame(canvas, bg='white', width=200, height=1000)
canvas.create_window(100, 500, window=frame)

root.mainloop()

我已经解决了python和tkinter在笔记本和滚动条中插入表单时遇到的问题。我要感谢@A.Rodas的热情帮助。在

这是我的代码,希望你觉得有用!在

import Tkinter
from Tkinter import *
from ttk import *
import tkMessageBox
import math 
import ttk 
import Tkinter as tk

def defocus(event):
    event.widget.master.focus_set()

# start of GUI code
root = Tk()
root.title("Model A")
root.resizable(0,0)

# start of Notebook (multiple tabs)
notebook = ttk.Notebook(root)
notebook.pack(fill=BOTH, expand=True)
notebook.pressed_index = None

# Child frames
ContainerOne = Frame(notebook)
ContainerOne.pack(fill=BOTH, expand=True)
ContainerTwo = Frame(notebook)
ContainerTwo.pack(fill=BOTH, expand=True)

# Create the pages
notebook.add(ContainerOne, text='Mode A')
notebook.add(ContainerTwo, text='Mode B')

canvas1 = Canvas(ContainerOne, width=1200, height=450)
scroll = Scrollbar(ContainerOne, command=canvas1.yview)
canvas1.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas1.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)
canvas2 = Canvas(ContainerTwo, width=1200, height=450)
scroll = Scrollbar(ContainerTwo, command=canvas2.yview)
canvas2.config(yscrollcommand=scroll.set, scrollregion=(0,0,100,1000))
canvas2.pack(side=LEFT, fill=BOTH, expand=True)
scroll.pack(side=RIGHT, fill=Y)

frameOne = Frame(canvas1, width=800, height=450)
canvas1.create_window(250, 125, window=frameOne)

frameTwo = Frame(canvas2, width=800, height=450)
canvas2.create_window(200, 140, window=frameTwo)

# Main Frame

#Close Application Button
def quit(root):
    root.destroy()

ttk.Button(root, text="Close Application", command=lambda root=root:quit(root)).pack()

if __name__ == '__main__':
    #Main Part
    stepOne = Tkinter.LabelFrame(frameOne, text=" 1. Enter Main Details: ", font=("fixedsys", "16","bold italic"))
    stepOne.grid(row=0, columnspan=5, sticky='nsew', padx=5, pady=5, ipadx=5, ipady=5)
    stepTwo = Tkinter.LabelFrame(frameOne, text=" 2. Calculate AP : ", font=("fixedsys", "16","bold italic"))
    stepTwo.grid(row=2, columnspan=7, sticky='WE', padx=5, pady=5, ipadx=5, ipady=5)

# First Step Starts 

# Component Selection
componentComb= ttk.Combobox(stepOne, width="19")
componentComb = Combobox(stepOne, state="readonly", values=("CDA", "VHS", "DVD"))
componentComb.grid(column=4, row=0, columnspan="5", sticky="nswe")
componentComb.set("Component Selection")

# Temperature Selection
tempComb = ttk.Combobox(stepOne, width="12")
tempComb = Combobox(stepOne, state="readonly", values=("-40", "-30", "-20","-10", "0",))
tempComb.grid(column=0, row=2, columnspan="2", sticky="w")
tempComb.set("Temperature Selection")

# Second Step Starts
inEncLbl = Tkinter.Label(stepTwo, text="Oxide:")
inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2)

inEncTxt = Tkinter.Entry(stepTwo, width=6)
inEncTxt.grid(row=2, column=1, sticky='w', pady=2)

outEncLbl = Tkinter.Label(stepTwo, text="Density Rate (DR):")
outEncLbl.grid(row=2, column=5, padx=5, pady=2)

outEncTxt = Tkinter.Entry(stepTwo, width=6)
outEncTxt.grid(row=2, column=7,sticky='w', pady=2)

#End Code
root.mainloop()

相关问题 更多 >